package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.come.Frame.ZhuFrame;
import org.come.bean.PathPoint;
import org.come.mouslisten.QuackGameMouslisten;
import org.come.until.CutButtonImage;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.QuackGameBtn;
import com.tool.image.ImageMixDeal;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;

 

public class QuackGameJpanel extends JPanel {

    public int kyNum;// 剩余的刮刮乐卡数量
    private JLabel[] labSysNum = new JLabel[5]; // 能中奖的召唤兽图片
    private JLabel[] labOpenNum = new JLabel[20]; // 本次抽奖的召唤兽图片
    private JLabel[] labBorder = new JLabel[20]; // 边框图片
    private JLabel labBs // 倍数图片
            ; // 问号
    private QuackGameBtn gameBtn, labWh; // 一键召唤按钮

    public QuackGameJpanel() {
        this.setPreferredSize(new Dimension(400, 400));
        this.setBackground(UIUtils.Color_BACK);
        this.setLayout(null);
        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 71);
        offBtn.setBounds(400 - 37, 10, 25, 25);
        this.add(offBtn);

        // 一键召唤按钮
        gameBtn = new QuackGameBtn("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "一键召唤", this);
        gameBtn.setBounds(250, 360, 99, 24);
        this.add(gameBtn);

        // 倍数图片
        labBs = new JLabel("", SwingConstants.CENTER);
        labBs.setBounds(313, 141, 35, 35);
        labBs.setForeground(Color.WHITE);
        labBs.setFont(UIUtils.nameFont);
        labBs.setIcon(icon2);
        labBs.setIcon(CutButtonImage.getImage("inkImg/hongmu/2/101_png.xy2uiimg.pet_def.png", -1, -1));
        this.add(labBs);

        // 问号
        labWh = new QuackGameBtn("skin/8100226/button/19.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "?", this);
        // labWh.setBounds(331, 110, 18, 18);
        this.add(labWh);
        // 能中奖的召唤兽图片
        for (int i = 0; i < 5; i++) {
            labSysNum[i] = new JLabel();
            labSysNum[i].setBounds(48 + i * 66, 50, 35, 35);
            this.add(labSysNum[i]);
        }
        // 本次抽奖的召唤兽图片
        for (int i = 0; i < 20; i++) {
            int row = i % 5;
            int col = i / 5;
            labOpenNum[i] = new JLabel();
            labOpenNum[i].setIcon(icon2);
            labOpenNum[i].setBounds(40 + row * 51, 140 + col * 51, 35, 35);
            this.add(labOpenNum[i]);
            labBorder[i] = new JLabel("", SwingConstants.CENTER);
            labBorder[i].setBounds(34 + row * 51, 133 + col * 51, 48, 48);
            labBorder[i].addMouseListener(new QuackGameMouslisten(i, this));
            this.add(labBorder[i]);
        }
    }

    private ImageIcon icon2 = new ImageIcon(CutButtonImage
            .getImage("inkImg/hongmu/2/101_png.xy2uiimg.pet_def.png", -1, -1).getImage().getScaledInstance(35, 35, 10));
    public String[] sysNum = null; // 能中奖的五个号码
    public String[] sysNum2 = null; // 能中奖的五个号码
    public String[] drawNum = null; // 抽中的号码
    public BigDecimal money = null; // 中奖金额
    public List<PathPoint> points = null;
    private long time;
    private int index;

    /** 重置 */
    public void reset() {
        for (int i = 0; i < labOpenNum.length; i++) {
            labOpenNum[i].setIcon(icon2);
        }
        points = null;
        labBs.setIcon(icon2);
    }

    /** 重置守护 */
    public void resetGuard(String[] v) {
        sysNum = v;
        for (int i = 0; i < v.length; i++) {
            ImageIcon icon = CutButtonImage.getImage("img/newHead/p" + v[i] + ".png", -1, -1);
//            ImageIcon icon = CutButtonImage.getImage(imageMap.getImgMap().get(v[i]), v[i] ,-1, -1);
            labSysNum[i].setIcon(icon);
        }
    }

    /** 开奖 */
    public void drawLottery(String[] v, BigDecimal moeny) {
        this.drawNum = v;
        this.money = moeny;
        this.time = 0;
        this.index = 0;
        if (points != null) {
            points.clear();
        }
        sysNum2 = new String[sysNum.length];
        System.arraycopy(sysNum, 0, sysNum2, 0, 5);
    }

    private ImageIcon icon;
    static Sprite tcp = SpriteFactory.VloadSprite("resource/mouse/flicker.tcp", null);

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (icon == null) {
            icon = new ImageIcon("inkImg/newbackground/B321.png");
        }
        // 道具栏
        g.drawImage(icon.getImage(), 0, 0, 400, 400, this);
        // 画剩余数量
        g.setColor(Color.white);
        g.setFont(UIUtils.TEXT_FONT1);
        g.drawString(kyNum + "", 116, 377);
        if (drawNum != null) {
            time++;
            if (time % 10 == 0) {
                ImageIcon icon = null;
                if (!"0".equals(drawNum[index])) {
                    icon = CutButtonImage.getImage("img/newHead/p" + drawNum[index] + ".png", -1, -1);
//                    icon = CutButtonImage.getImage(imageMap.getImgMap().get(drawNum[index]),drawNum[index], -1, -1);
                }
                if (index == 20) {
                    labBs.setIcon(icon);
                } else {
                    for (int i = 0; i < sysNum2.length; i++) {
                        if (sysNum2[i] != null && sysNum2[i].equals(drawNum[index])) {
                            sysNum2[i] = null;
                            if (points == null) {
                                points = new ArrayList<>();
                            }
                            int row = index % 5;
                            int col = index / 5;
                            points.add(new PathPoint(34 + row * 51, 133 + col * 51));
                        }
                    }
                    labOpenNum[index].setIcon(icon);
                }
                index++;
                if (index > 20) {
                    gameBtn.setBtn(1);
                    gameBtn.setText("再来一次");
                    if ("400151".equals(drawNum[drawNum.length - 1])) {// 金灵鼠
                        ZhuFrame.getZhuJpanel().addPrompt("你遇到了金灵鼠， 硕鼠硕鼠，勿食我黍。本次颗粒无收！！");
                    } else if (money != null && money.intValue() != 0) {
                        ZhuFrame.getZhuJpanel().addPrompt("获得了" + money + "银两");
                    }
                    drawNum = null;
                }
            }
        }
        if (points != null) {
            tcp.updateToTime(ImageMixDeal.userimg.getTime(), 0);
            for (int i = 0; i < points.size(); i++) {
                tcp.draw(g, points.get(i).getX(), points.get(i).getY());
            }
        }
    }

    public JLabel[] getLabSysNum() {
        return labSysNum;
    }

    public void setLabSysNum(JLabel[] labSysNum) {
        this.labSysNum = labSysNum;
    }

    public JLabel[] getLabOpenNum() {
        return labOpenNum;
    }

    public void setLabOpenNum(JLabel[] labOpenNum) {
        this.labOpenNum = labOpenNum;
    }

    public JLabel[] getLabBorder() {
        return labBorder;
    }

    public void setLabBorder(JLabel[] labBorder) {
        this.labBorder = labBorder;
    }

    public JLabel getLabBs() {
        return labBs;
    }

    public void setLabBs(JLabel labBs) {
        this.labBs = labBs;
    }

    public QuackGameBtn getLabWh() {
        return labWh;
    }

    public void setLabWh(QuackGameBtn labWh) {
        this.labWh = labWh;
    }

    public QuackGameBtn getGameBtn() {
        return gameBtn;
    }

    public void setGameBtn(QuackGameBtn gameBtn) {
        this.gameBtn = gameBtn;
    }

    public ImageIcon getIcon2() {
        return icon2;
    }

    public void setIcon2(ImageIcon icon2) {
        this.icon2 = icon2;
    }
}
